using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFFSM;
using XFGameFramework;
using UnityEngine.SceneManagement;
using System;
public class Utility
{
    private Module module;
    private LoadingPanel loadingPanel;
    /// <summary>
    /// 根据给定的概率，判断事件是否触发
    /// </summary>
    /// <param name="probability">发生概率 (0 ~ 1)</param>
    /// <returns>是否触发</returns>
    public static bool IsTriggered(float probability)
    {
        if (probability <= 0) return false; // 0% 概率，绝对不触发
        if (probability >= 1) return true;  // 100% 概率，绝对触发

        return UnityEngine.Random.value < probability; // Random.value 生成 0~1 之间的随机数
    }

    /// <summary>
    /// 根据给定的权重列表，随机选择一个ID
    /// </summary>
    /// <param name="encoutnerLikelihood"></param>
    /// <returns></returns>
    /// <exception cref="InvalidOperationException"></exception>
    public static int GetRandomId(List<KeyValue<int, float>> encoutnerLikelihood)
    {
        if (encoutnerLikelihood == null || encoutnerLikelihood.Count == 0)
            throw new InvalidOperationException("列表为空，无法进行随机选择！");

        float totalWeight = 0;
        foreach (var item in encoutnerLikelihood)
        {
            totalWeight += item.Likelihood;
        }

        // 生成 [0, totalWeight) 之间的随机数
        System.Random random = new System.Random();
        float randValue = (float)(random.NextDouble() * totalWeight);

        // 进行加权随机抽取
        float cumulativeWeight = 0;
        foreach (var item in encoutnerLikelihood)
        {
            cumulativeWeight += item.Likelihood;
            if (randValue < cumulativeWeight)
            {
                return item.Id;  // 选中该ID
            }
        }
        // 如果发生意外情况（理论上不该发生），返回最后一个ID
        return encoutnerLikelihood[encoutnerLikelihood.Count - 1].Id;
    }
    /// <summary>
    /// 根据概率分布随机选择一个事件ID（包含无事件的情况）
    /// </summary>
    /// <param name="eventProbabilities">事件列表（ID + 发生概率）</param>
    /// <returns>触发的事件ID，-1表示没有触发任何事件</returns>
    public static int GetRandomEvent(List<KeyValue<int, float>> eventProbabilities)
    {
        if (eventProbabilities == null || eventProbabilities.Count == 0)
            return -1; // 直接返回无事件

        // 验证概率总和不超过1
        float totalProbability = 0f;
        foreach (var item in eventProbabilities)
        {
            if (item.Likelihood < 0)
                throw new ArgumentException("概率值不能为负数");

            totalProbability += item.Likelihood;

            if (totalProbability > 1f)
                throw new InvalidOperationException("概率总和超过1（100%）");
        }

        // 生成0~1的随机数（使用Unity的随机数生成器）
        float randomValue = UnityEngine.Random.value;

        // 遍历所有可能的事件
        float cumulative = 0f;
        foreach (var eventData in eventProbabilities)
        {
            cumulative += eventData.Likelihood;
            if (randomValue < cumulative)
            {
                return eventData.Id; // 命中该事件
            }
        }

        // 没有命中任何事件（剩余概率部分）
        return -1;
    }
    /// <summary>
    /// 非超凡属性的属性鉴定方法
    /// </summary>
    /// <param name="判定值">判定值</param>
    /// <param name="鉴定值">鉴定值</param>
    /// <returns></returns>
    public static bool 属性鉴定(int 判定值, int 鉴定值)
    {
        if (判定值 / 2 + UnityEngine.Random.Range(0, 判定值) >= 鉴定值)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static bool 属性鉴定_超凡(int 判定值, int 鉴定值)
    {
        if (10 + (判定值 - 20) + UnityEngine.Random.Range(0, 判定值) >= 鉴定值)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

}
